#include <iostream>
using namespace std;
class Person {
public:
  int a;
  void* operator new(size_t n) {
    void* ptr;
    cout << "i am operator new" << endl;
    cout << "n: " << n << endl;
    ptr = (Person*)malloc(n);
    return ptr;
  } 
  void operator delete(void* ptr) {
    cout << "i am operator delete" << endl;
    free(ptr);
  }
  Person() {
    cout << "Person()" << endl;
  }
  ~Person() {
    cout << "~Person()" << endl;
  }
};
int main()
{
  //int* p1 = (int*)malloc(sizeof(int));
  //*p1 = 99;
  //cout << *p1 << endl;
  //free(p1);
  
  //int* p1 = new int(88);
  //cout << *p1 << endl;
  //delete p1;
  
  //int* p1  =(int*)malloc(sizeof(int)*10);
  //for(int i = 0; i < 10; i++) {
  //  *(p1+i) = i;
  //}
  //for(int i = 0; i < 10; i++) {
  //  cout << *(p1+i) << endl;
  //}
  //free(p1);
  
  //int* p1 = new int[10];
  //for(int i = 0; i < 10; i++) {
  //  *(p1+i) = i;
  //}
  //for(int i = 0; i < 10; i++) {
  ////  cout << *(p1+i) << endl;
  //    cout << p1[i] << endl;
  //}
  //delete[] p1;
  
  //Person* p1 = (Person*)malloc(sizeof(Person));
  //free(p1);
  
  // 1. new == operator new + 构造函数
  // 2. delete == 析构函数 + operator delete 
  // 3. operator new == malloc + 失败抛异常
  // 4. operator delete = free + 失败抛异常
  
  Person* p1 = new Person;
  delete p1;

  return 0;
}
